<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<!-- 条件搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" setup>
defineOptions({
	name: "pay-payment",
});

import { useCrud, useTable, useUpsert, useSearch } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { useI18n } from "vue-i18n";
import { reactive } from "vue";
import UserSelect from "/$/user/components/user-select.vue";

const { service } = useCool();
const { t } = useI18n();

// 选项
const options = reactive({
	paymentMethod: [
		{ label: t("微信支付"), value: 0 },
		{ label: t("支付宝"), value: 1 },
		{ label: t("余额支付"), value: 2 },
		{ label: t("银行卡"), value: 3 },
	],
	status: [
		{ label: t("待支付"), value: 0 },
		{ label: t("支付中"), value: 1 },
		{ label: t("支付成功"), value: 2 },
		{ label: t("支付失败"), value: 3 },
		{ label: t("已退款"), value: 4 },
	],
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: t("选择用户"),
			prop: "userId",
			component: { vm: UserSelect },
			required: true,
		},
		{
			label: t("选择订单"),
			prop: "orderId",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
			required: true,
		},
		{
			label: t("支付流水号"),
			prop: "paymentNo",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
			required: true,
		},
		{
			label: t("第三方支付流水号"),
			prop: "thirdPartyNo",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("支付金额"),
			prop: "amount",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
			required: true,
		},
		{
			label: t("支付方式"),
			prop: "paymentMethod",
			component: {
				name: "el-radio-group",
				options: options.paymentMethod,
			},
			value: 0,
			required: true,
		},
		{
			label: t("支付状态"),
			prop: "status",
			component: {
				name: "cl-select",
				props: { options: options.status },
			},
			value: 0,
			span: 12,
			required: true,
		},
		{
			label: t("支付时间"),
			prop: "payTime",
			component: {
				name: "el-date-picker",
				props: { type: "datetime", valueFormat: "YYYY-MM-DD HH:mm:ss" },
			},
			span: 12,
		},
		{
			label: t("支付完成时间"),
			prop: "completeTime",
			component: {
				name: "el-date-picker",
				props: { type: "datetime", valueFormat: "YYYY-MM-DD HH:mm:ss" },
			},
			span: 12,
		},
		{
			label: t("退款时间"),
			prop: "refundTime",
			component: {
				name: "el-date-picker",
				props: { type: "datetime", valueFormat: "YYYY-MM-DD HH:mm:ss" },
			},
			span: 12,
		},
		{
			label: t("退款金额"),
			prop: "refundAmount",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("支付渠道"),
			prop: "payChannel",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("支付IP"),
			prop: "payIp",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("支付设备信息"),
			prop: "deviceInfo",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("第三方返回信息"),
			prop: "thirdPartyResponse",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("失败原因"),
			prop: "failReason",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("备注"),
			prop: "remark",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("支付前余额"),
			prop: "beforeBalance",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("支付后余额"),
			prop: "afterBalance",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
	],
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: t("订单号"), prop: "orderNo", minWidth: 120 },
		{ label: t("订单标题"), prop: "orderTitle", minWidth: 120 },
		{ label: t("手机号"), prop: "userPhone", minWidth: 120 },
		{ label: t("昵称"), prop: "userNickName", minWidth: 120 },
		{ label: t("订单ID"), prop: "orderId", minWidth: 120 },
		{ label: t("支付流水号"), prop: "paymentNo", minWidth: 120 },
		{ label: t("第三方支付流水号"), prop: "thirdPartyNo", minWidth: 120 },
		{ label: t("支付金额"), prop: "amount", minWidth: 120 },
		{
			label: t("支付方式"),
			prop: "paymentMethod",
			minWidth: 120,
			dict: options.paymentMethod,
		},
		{
			label: t("支付状态"),
			prop: "status",
			minWidth: 120,
			dict: options.status,
		},
		{
			label: t("支付时间"),
			prop: "payTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" },
		},
		{
			label: t("支付完成时间"),
			prop: "completeTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" },
		},
		{
			label: t("退款时间"),
			prop: "refundTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" },
		},
		{ label: t("退款金额"), prop: "refundAmount", minWidth: 120 },
		{ label: t("支付渠道"), prop: "payChannel", minWidth: 120 },
		{ label: t("支付IP"), prop: "payIp", minWidth: 120 },
		{ label: t("支付设备信息"), prop: "deviceInfo", minWidth: 120 },
		{
			label: t("第三方返回信息"),
			prop: "thirdPartyResponse",
			minWidth: 120,
		},
		{ label: t("失败原因"), prop: "failReason", minWidth: 120 },
		{ label: t("备注"), prop: "remark", minWidth: 120 },
		{ label: t("支付前余额"), prop: "beforeBalance", minWidth: 120 },
		{ label: t("支付后余额"), prop: "afterBalance", minWidth: 120 },
		{
			label: t("创建时间"),
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" },
		},
		{
			label: t("更新时间"),
			prop: "updateTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" },
		},
		{ type: "op", buttons: ["edit", "delete"] },
	],
});

// cl-search
const Search = useSearch();

// cl-crud
const Crud = useCrud(
	{
		service: service.pay.payment,
	},
	(app) => {
		app.refresh();
	},
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
